package com.mano233.server.tool;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTCreationException;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.JWTVerifier;

import java.util.Map;

/**
 * JWT验证工具类
 *
 * @author mano233
 */
public class JwtUtil {
    private static Algorithm algorithmHS = Algorithm.HMAC256("my_secret");

    /**
     * 签名
     * @param userID 用户ID
     * @return token
     */
    public static String signToken(int userID) throws JWTCreationException {
        return JWT.create()
                .withIssuer("easyEvent.io")
                .withClaim("userId", userID)
                .sign(algorithmHS);
    }

    /**
     * 解析jwt
     * @param token token字符串
     */
    public static DecodedJWT verifyToken(String token) throws JWTVerificationException {
        // Reusable verifier instance
        JWTVerifier verifier = JWT.require(algorithmHS)
                .withIssuer("easyEvent.io")
                .build();
        return verifier.verify(token);
    }

}
